package vi.org.rongcloud19test;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import io.rong.imkit.RongIM;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.ipc.RongExceptionHandler;
import io.rong.message.LocationMessage;
import io.rong.message.RichContentMessage;
import vi.org.rongcloud19test.utils.DemoContext;
import vi.org.rongcloud19test.utils.TextMessageItemProvider;

/**
 * Created by Vicky on 2015/11/19.
 * RongCloud19Test
 * contact way: 317461087@qq.com
 */
public class App extends Application {
    @Override
    public void onCreate() {
        super.onCreate();

        /**
         * 只有在使用RongIM的进程和push进程才会执行init
         */
        if(getApplicationInfo().packageName.equals(getCurProcessName(getApplicationContext()))
                ||"io.rong.push".equals(getCurProcessName(getApplicationContext())))
        {
            RongIM.init(this);
            /**
             * 融云SDK事件监听处理
             *
             * 注册相关代码，只需要在主进程里做。
             */
            if (getApplicationInfo().packageName.equals(getCurProcessName(getApplicationContext()))) {

                //RongCloudEvent.init(this);
                DemoContext.init(this);

                Thread.setDefaultUncaughtExceptionHandler(new RongExceptionHandler(this));

                try {
                    /*RongIM.registerMessageType(DeAgreedFriendRequestMessage.class);

                    RongIM.registerMessageTemplate(new ContactNotificationMessageProvider());
                    RongIM.registerMessageTemplate(new RealTimeLocationMessageProvider());*/
                    RongIM.registerMessageTemplate(new TextMessageItemProvider());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        /*RongIM.setConversationBehaviorListener(new RongIM.ConversationBehaviorListener() {

            @Override
            public boolean onClickUserPortrait(Context context, RongIMClient.ConversationType conversationType, RongIMClient.UserInfo user) {

                //在这里处理你想要跳转的activity，示例代码为YourAcitivy

                Intent in = new Intent(context, YourAcitivy.class);
                context.startActivity(in);
                return false;
            }

            @Override
            public boolean onMessageClick(Context context, RongIMClient.Message message) {

                //点击消息处理事件，示例代码展示了如何获得消息内容
                if (message.getContent() instanceof LocationMessage) {
                    Intent intent = new Intent(context, LocationActivity.class);
                    intent.putExtra("location", message.getContent());
                    context.startActivity(intent);

                }else  if(message.getContent() instanceof RichContentMessage){
                    RichContentMessage  mRichContentMessage = (RichContentMessage) message.getContent();
                    Log.d("Begavior", "extra:" + mRichContentMessage.getExtra());

                }

                Log.d("Begavior", message.getObjectName() + ":" + message.getMessageId());

                return false;
            }
        });*/
    }

    /**
     * 获得当前进程的名字
     *
     * @param context
     * @return 进程号
     */
    public static String getCurProcessName(Context context) {

        int pid = android.os.Process.myPid();

        ActivityManager activityManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);

        for (ActivityManager.RunningAppProcessInfo appProcess : activityManager
                .getRunningAppProcesses()) {

            if (appProcess.pid == pid) {
                return appProcess.processName;
            }
        }
        return null;
    }
}
